package com.kdg.designModel;

/**
 * Created by kong on 2017/9/8.
 * 策略模式：定义了算法家族，分别封装起来让他们之间可以互相替换，
 * 此模式让算法的变化不会影响到使用算法的客户
 */
public class MarketPay{
    public static void main(String[] args) {
        payCashByFactory();
        payCashByStrategeFactory();
    }

    private static void payCashByStrategeFactory() {
        CashContext cc = new CashContext("打8折");
        double totalPrice = cc.GetResult(800);
        System.out.println(totalPrice);
    }

    private static void payCashByFactory() {
        CashSuper cashSuper = CashFactory.createCashSuper("打8折");
        double totalPrice = cashSuper.acceptCash(800);
        System.out.println(totalPrice);
    }
}

/**
 * 简单工厂类
 */
class CashFactory{
    public static CashSuper createCashSuper(String type){
        if("".equals(type) || type == null){
            return null;
        }
        CashSuper cs = null;
        switch (type){
            case "正常收费" :
                cs = new CashNormal();
                break;
            case "满300减100活动" :
                cs =  new CashReturn(300,100);
                break;
            case "打8折" :
                cs =  new CashRebate(0.8);
                break;
        }
        return cs;

    }
}
/**
 * 策略类
 */
class CashContext{
    CashSuper cashSuper;

    public CashContext(String type) {
        CashSuper cs = null;
        switch (type){
            case "正常收费" :
                cs = new CashNormal();
                break;
            case "满300减100活动" :
                cs =  new CashReturn(300,100);
                break;
            case "打8折" :
                cs =  new CashRebate(0.8);
                break;
        }
        this.cashSuper = cs;
    }

    public double GetResult(double money){
        return cashSuper.acceptCash(money);
    }
}

/**
 * 现金收费抽象类
 */
abstract class CashSuper{
    public abstract double acceptCash(double money);
}

/**
 * 正常收费子类
 */
class CashNormal extends CashSuper{

    @Override
    public double acceptCash(double money) {
        return money;
    }
}

/**
 * 打折子类
 */
class CashRebate extends CashSuper{

    private double moneyRebate = 1d;

    public CashRebate(double moneyRebate) {
        this.moneyRebate = moneyRebate;
    }

    @Override
    public double acceptCash(double money) {
        return money*moneyRebate;
    }
}

/**
 * 买多少返多少子类
 */
class CashReturn extends CashSuper{

    private double moneyCondition = 0d;
    private double moneyReturn = 0d;

    public CashReturn(double moneyCondition, double moneyReturn) {
        this.moneyCondition = moneyCondition;
        this.moneyReturn = moneyReturn;
    }

    @Override
    public double acceptCash(double money) {
        double allMoney = money;
        if(money >= moneyCondition){
            allMoney = money - Math.floor(money / moneyCondition) * moneyReturn;
        }
        return allMoney;
    }
}
